package com.lkc.controller;

import com.lkc.domain.ResponseResult;
import com.lkc.domain.entity.User;
import com.lkc.enums.AppHttpCodeEnum;
import com.lkc.exception.SystemException;
import com.lkc.service.UserService;
import com.lkc.utils.security.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Title: UserController
 * @Author 椰咩咩
 * @Package com.lkc.controller
 * @Date 2022/12/24 16:18
 * @description: 用户（User）的控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/students")
    public ResponseResult students(Integer pageNum, Integer pageSize) {
        return userService.students(pageNum, pageSize);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delStudent(@PathVariable Long id) {
        if (!LoginUser.isTeacher()) {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        boolean b = userService.removeById(id);
        return b ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @GetMapping("/{id}")
    public ResponseResult getStudentById(@PathVariable Long id) {
        return userService.getStudentById(id);
    }

    @PostMapping
    public ResponseResult addStudent(@RequestBody User student) {
        return userService.addStudent(student);
    }

    @PutMapping
    public ResponseResult updateStudent(@RequestBody User student) {
        return userService.updateStudent(student);
    }
}
